import 'package:aoe_flutter/contants/constants.dart';
import 'package:aoe_flutter/utils/dio_client.dart' ;
import 'package:aoe_flutter/widgets/tips/tips_tool.dart';
import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:dio/dio.dart'show FormData;

import '../basic_app.dart';

class LoginControl {
  LoginControl._();

  static LoginControl _instance;

  static LoginControl getInstance() {
    
    if (_instance == null) {
      _instance = LoginControl._();
    }
    return _instance;
  }

  Future<bool> login(String username, String password) async {
    Map<String, String> params = {
      LOGIN_USERNAME_PARAM_KEY: username,
      LOGIN_PASSWORD_PARAM_KEY: password,
    };
    // ResponseBody response = await DioClient().post('/login', params: params);
     FormData formData =
          new FormData.fromMap({'userName': username, 'password': password});
     ResponseBody response = await DioClient().post(
          app.baseUrl + '/api/wsgw/webservice/user/Login',
          params: formData);
      if (response.success) {
        UserInfo userInfo = response.data<UserInfo>();
        app.global.setUserInfo(JsonMapper.serialize(userInfo));
        app.userInfo = userInfo;
        TipsTool.warning("欢迎您，"+userInfo.userName);
        return true;
      }else{
        TipsTool.warning(response.error);
        return false;
      }
  }

  

  Future<void> logOut() async {
    ResponseBody responseBody = await DioClient().get('/logout');
    if (responseBody.success) {
      // app.global.clear();
      // app.messageBox.clear();
      // app.userInfo = null;
      // app.socketClient.closeSocket();
      // app.navigatorKey.currentState
      //     .pushNamedAndRemoveUntil('loginPage', (route) => route == null);
    } else {
      // TipsTool.warning(responseBody.message).show();
    }
  }
}

@jsonSerializable
class UserInfo {
 @JsonProperty(name: 'userId')
  String userId;
  String userName;
  String displayName;
  String password;
  int status;
  List<String> bundles;
  @JsonProperty(ignore: true)
  String error;
  @JsonProperty(ignore: true)
  List loginUserList;
  @JsonProperty(ignore: true)
  List<UserRole> roleList;

  UserInfo(this.userId, this.userName, this.password, this.displayName,this.bundles,
      this.status,{ this.roleList,this.error});
}


class UserRole {
  String roleCode;
  String roleName;
  UserRole(this.roleCode, this.roleName);
}